package servlet;

import pojo.Brand;
import service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddServlet ",value = "/add")
public class AddServlet extends HttpServlet {
    private BrandService brandService = new BrandService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1.设置处理post请求乱码的字符集
        request.setCharacterEncoding("utf-8");
//        2.接收客户端提交的数据:brandName,companyName,ordered,description,status
        String brandName = request.getParameter("brandName");
        String companyName = request.getParameter("companyName");
        String ordered = request.getParameter("ordered");
        String description = request.getParameter("description");
        String status = request.getParameter("status");
//        3.将接收到的数据封装到 Brand 对象中
        Brand brand = new Brand();
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setOrdered(Integer.valueOf(ordered));
        brand.setDescription(description);
        brand.setStatus(Integer.valueOf(status));
//        4.调用 BrandService 的 add() 方法进行添加的业务逻辑处理
        brandService.add(brand);
//        5.跳转到 selectAllServlet 重新查询数据
        response.sendRedirect("selectAll");
    }
}
